//
//  CZAccount.m
//  小和微博
//
//  Created by 董晓合 on 15/2/25.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "CZAccount.h"
#import <objc/runtime.h>
#import <objc/message.h>

#define CZAccountFileName @"account.data"

@implementation CZAccount

MJCodingImplementation

- (BOOL)save
{
    // 1.获取沙河路径
    NSString *accountPath = [CZAccountFileName appendDocumentDir];
    
    // 2.生成真正的过期时间
    NSDate *now = [NSDate date];
    self.expires_time = [now dateByAddingTimeInterval:[self.expires_in doubleValue]];
    DDLogDebug(@"%@", self.expires_time);
    
    // 2.将自己存储起来
    return [NSKeyedArchiver archiveRootObject:self toFile:accountPath];
}

+ (instancetype)accountFromSandbox
{
    // 1.获取沙盒路径
    NSString *accountPath = [CZAccountFileName appendDocumentDir];
    
    // 2.取出存储的模型对象
    CZAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:accountPath];
    
    // 3.判断授权是否过期
    NSDate *now = [NSDate date];
    // 2014-2-1    2020-1-31
    if ([now compare:account.expires_time] != NSOrderedAscending) {
        return nil;
    }
    
    // 3.返回模型对象
    return account;
    
}

@end
